<?php
function down($dir, $name, $ext, $islogin = false, $ren = 0) {
    if ($islogin) {
        @session_start();
        if (isset($_SESSION['member'])) {
        } else {
            exit("此资源，必须是会员才可以下载");
            return;
        }
    }
    $file = $dir . './' . $name . $ext;
    if (file_exists($file)) {
        $f = fopen($file, 'r');
        $len = filesize($file);
        header("Content-type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Accept-Length:" . $len);
        switch ($ren) {
            case 1:
                $name = date('YmdHis');
                break;
            case 2:
                $name = uniqid();
                break;
        }
        header("Content-Disposition:attachment;filename=" . $name . $ext);
        $buf = 1024;
        $count = 0;
        while (!feof($f) && $count < $len) {
            $r = fread($f, $buf);
            $count += $buf;
            echo $r;
        }
        fclose($f);
        return;
    } else {
        exit("未知资源，无法下载！！！");
        return;
    }
}

down($_GET['dir'], $_GET['name'], $_GET['ext'], false, 1);